package hongcaosp.app.android.search;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.comment.LoadMoreHandler;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.modle.bean.Video;
import hongcaosp.app.android.video.player.VideoPresenter;
import hongcaosp.app.android.video.player.VideosPlayActivity;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class SearchVideoFragment extends BaseFragment implements SearchEventListener,OnRecycleItemClickListener,IListView<Video> {
    private RecyclerView recyclerView;
    private BaseAdapter<Video> adapter;

    private LinearLayoutManager linearLayoutManager;

    private SearchPresenter searchPresenter;

    private LoadMoreHandler loadMoreHandler;



    private View emptyToast;
    @Override
    public void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(linearLayoutManager =new GridLayoutManager(getContext(),2,LinearLayout.VERTICAL,false));
        recyclerView.setAdapter( adapter = new BaseAdapter<Video>() {
            @NonNull
            @Override
            public SearchVideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new SearchVideoHolder(getContext(),SearchVideoFragment.this);
            }
        });

        loadMoreHandler = new LoadMoreHandler(linearLayoutManager) {
            @Override
            public void loadMore() {
                searchPresenter.loadMore();
            }
        };
        recyclerView.addOnScrollListener(loadMoreHandler);
        emptyToast = findViewById(R.id.empty_toast);

        searchPresenter = new SearchPresenter(this,null);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_search_video;
    }

    @Override
    public void onStartSearch(String text) {
        searchPresenter.fresh(text);
    }

    @Override
    public void onItemClick(int position) {
        ArrayList<IVideo> videos = new ArrayList<>();
        videos.addAll(adapter.getData());
        VideosPlayActivity.toPlayVideos(getContext(),3,videos,searchPresenter.getNumber(),position,searchPresenter.getContent());
    }

    @Override
    public void onItemLongClick(int position) {

    }

    @Override
    public void freshList(List<Video> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        adapter.setData(data);
        adapter.notifyDataSetChanged();
        freshEmptyState();
    }

    @Override
    public void addList(List<Video> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        if (data != null) {
            adapter.addData(data);
            adapter.notifyDataSetChanged();
        }
        freshEmptyState();
    }

    private void freshEmptyState() {
        if (adapter.getItemCount() == 0) {
            emptyToast.setVisibility(View.VISIBLE);
        } else {
            emptyToast.setVisibility(View.GONE);
        }
    }
}
